package servlets;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import managers.DatabaseManager;
import managers.JSonManager;
import structures.User;

public class GetNormalUserServlet extends HttpServlet {

	private static final long serialVersionUID = -5879956500425194398L;

	//method to read
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		String email, password, jsonResult;
		User user;
		JSonManager jSonManager;
		jSonManager = new JSonManager();
		DatabaseManager databaseManager;
		databaseManager = new DatabaseManager();
		email = request.getParameter("email");
		password = request.getParameter("password");
		databaseManager.connect();
		user = databaseManager.getUserByCredentials(email, password);
		databaseManager.closeConnection();
		jsonResult = jSonManager.generateJSON(user);
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		try {
			//write the result as JSon
			response.getWriter().write(jsonResult);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
}
